/*
 * Filename PreOrderController.java 2022年10月12日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.controller;

import com.ehome.poct.core.api.constant.enums.ScanTypeEnums;
import com.ehome.poct.core.api.dto.groups.ValidationGroups;
import com.ehome.poct.core.api.dto.order.PrePlaceOrderDTO;
import com.ehome.poct.core.api.virtual.PreOrderService;
import com.ehome.poct.core.api.vo.order.OrderStatusVO;
import com.ehome.poct.core.api.vo.order.PreOrderPayReqDTO;
import com.ehome.poct.core.api.vo.order.PreOrderPayRspVO;
import com.ehome.poct.core.api.vo.order.PreOrderRspVO;
import com.ehome.poct.core.utils.ValidateUtil;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 支付前置
 *
 * @author wyc
 * @since 2022/10/12 13:47
 **/
@Api(tags = "支付前置")
@ApiSupport(author = "wyc")
@RestController
@RequiredArgsConstructor
@RequestMapping("/pre/order")
@Slf4j
public class PreOrderController {
    private final PreOrderService preOrderService;

    @ApiOperation("下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/place")
    @RequestLog
    public PreOrderRspVO place(@RequestBody PrePlaceOrderDTO preOrder) {
        return preOrderService.orderV2(preOrder);
    }

    @ApiOperation("重新下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/rePlace/{orderNo}")
    public PreOrderRspVO rePlace(@PathVariable String orderNo) {
        return preOrderService.reOrder(orderNo);
    }

    @ApiOperation("唤起支付")
    @ApiOperationSupport(author = "pxh")
    @PostMapping("/pay")
    @RequestLog
    public PreOrderPayRspVO pay(@RequestBody @Validated PreOrderPayReqDTO preOrderPayReqDTO) {
        log.info("pay request params:[{}]", preOrderPayReqDTO);

        if (Objects.isNull(preOrderPayReqDTO.getScanType())) {
            preOrderPayReqDTO.setScanType(ScanTypeEnums.C2B);
        }

        if (ScanTypeEnums.B2C.equals(preOrderPayReqDTO.getScanType())) {
            ValidateUtil.validate(preOrderPayReqDTO, ValidationGroups.FirstGroups.class);
        }
        return preOrderService.payV2(preOrderPayReqDTO);
    }

    @ApiOperation("主动查询订单状态")
    @ApiOperationSupport(author = "pxh")
    @GetMapping("/queryOrderStatus/{orderNo}")
    @RequestLog
    public OrderStatusVO queryOrderStatus(@PathVariable String orderNo) {
        return preOrderService.queryOrderStatus(orderNo);
    }
}